# -*- coding:utf-8 -*-

"""
@author: fanqc
@file: conf.py
@time: 2023/2/16 11:01
"""
import datetime
import errno
import logging
import os
import sys

import requests
from lxml import etree

import util
from util import get_inner_ip

TIME_WAIT_OUT = 10
TIME_CHECK = 0.5
STEP = 5
RETRY = 2
ON_MESSAGE = True
SHEET_NAME = "待匹配商品明细"
APP_NAME = "spider_allen"
MINE_IP = get_inner_ip()

url_jd_detail = 'https://item.jd.com/%s.html'
url_tb_home = 'https://login.taobao.com/member/login.jhtml?spm=a230r.7195193.754894437.1.5a8f30c09mXdyn&f=top&redirectURL=https%3A%2F%2Fshopsearch.taobao.com%2Fsearch%3Fq%3D%25E7%25BE%258E%25E4%25BB%2595%25E5%25BA%25B7%26js%3D1%26initiative_id%3Dstaobaoz_20230217%26ie%3Dutf8'
url_search_shop = 'https://shopsearch.taobao.com/browse/shop_search.htm?q=%s&commend=all&ssid=s5-e&search_type=shop&sourceId=tb.index&spm=a21bo.jianhua.201856-taobao-item.2&ie=utf8&initiative_id=tbindexz_20170306'
url_mall_search_product = "/?q=%s&type=p&search=y&newHeader_b=s_from&searcy_type=item&from=.shop.pc_2_searchbutton"
url_tb_search_product = "search.htm?q=%s&searcy_type=item&s_from=newHeader&source=&ssid=s5-e&search=y"
dingding_send = "https://oapi.dingtalk.com/robot/send?access_token=5e3d8f6bcae87e59210e2dc3b8f7aaa8cddd5593f43092b5f3483e1e9a55882a"

TEXT = \
    '''
    杜甫《望岳》

    岱宗夫如何？齐鲁青未了。
    造化钟神秀，阴阳割昏晓。
    荡胸生曾云，决眦入归鸟。( 曾 同：层)
    会当凌绝顶，一览众山小。
    '''
COPYRIGHT = \
    '''
    注意事项： 
       1、需安装chrome浏览器（版本号109以上）
       2、请将excel源文件表头设置成图片（excel.jpeg）所示（顺序、大小写必须一致, 建议从demo.xlsx文件复制）（目的：标记防止重复采集）
       3、商品序号必须唯一，且有序（顺序为： 商品序号 = 表格行号 - 1）
       4、表sheet名称必须为（待匹配商品明细）
       
    声明：本软件为开发者用于研究学习，禁止用于商业行为，违者自行承担由此产生的不利后果
    copyright @fanqc 
    '''

jd_cookies = {
    'shshshfp': 'eea254159389ce131c501b927ebae568',
    'shshshfpa': '74724174-5869-501e-cc9c-21c9a1959069-1677040493',
    'shshshfpx': '74724174-5869-501e-cc9c-21c9a1959069-1677040493',
    'shshshfpb': 'xtX-e9Ex9X_L4eXjhFmHOtw',
    'jsavif': '1',
    'jsavif': '1',
    '__jda': '122270672.16770405088851761760721.1677040509.1677040509.1677040509.1',
    '__jdc': '122270672',
    '__jdv': '122270672|direct|-|none|-|1677040508886',
    'areaId': '22',
    '__jdu': '16770405088851761760721',
    'ip_cityCode': '1930',
    'ipLoc-djd': '22-1930-50948-71603',
    'token': 'c8d23dfafdc916a3f1ee09199ec0b9b2,2,931689',
    '__tk': 'rw15rwaE2wqE2w2trwgtKws3qA1B2US3rYrYrwVA2uTY2zM3rYrtrn,2,931689',
    'shshshsID': '8f9b013e9ff505e0f4ce72196b2de205_4_1677040867045',
    '__jdb': '122270672.3.16770405088851761760721|1.1677040509',
    '3AB9D23F7A4B3C9B': 'Y3TUFD6YK2EMABXO4EZ66ZOB7Y45MJTPI2LZKNF4SEUAB2IGIYQVBHPAWMSDKHMUEEKMDJGTQSDX5TD5ZL43WLNGMY',
}

jd_headers = {
    'authority': 'item.jd.com',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    'pragma': 'no-cache',
    'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'document',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'none',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
}


def update_jd_cookies(excel_path: str, cookies_filename='jd_cookies.txt'):
    global jd_cookies, jd_headers
    try:
        resp = requests.get('https://item.jd.com/10067011152496.html', headers=jd_headers, cookies=jd_cookies)
        resp.encoding = 'utf-8'
        html = etree.HTML(resp.text)
        title = html.xpath("//title")

        if '验证码中间页' in title[0].text.strip():
            name = excel_path.rsplit(os.sep)[-1]
            with open(excel_path.replace(name, cookies_filename), 'r') as f:
                jd_cookies = util.str_to_dict(f.read())
                log.info(f"====> 京东cookies更新成功 \n {jd_cookies}")
        else:
            log.info(f"====> 京东cookies仍有效 无需更新")
    except Exception as e:
        log.error(e)


def get_desktop_path():
    return os.path.join(os.path.expanduser("~"), 'Desktop')


ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
RESOURCE_PATH = os.path.join(ROOT_PATH, 'resources')
DESKTOP_PATH = get_desktop_path()
R_ROOT_PATH = os.path.join(DESKTOP_PATH, 'spider_product')
JD_RESULT_PATH = os.path.join(R_ROOT_PATH, 'jd_result.xlsx')
TB_RESULT_PATH = os.path.join(R_ROOT_PATH, 'tb_result.xlsx')
ERROR_PATH = os.path.join(R_ROOT_PATH, 'error.xlsx')
file_app_format = "[%(asctime)s][%(name)s][%(levelname)s] %(message)s "


class LoggingConfig(object):
    """
    日志配置
    """
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(LoggingConfig, cls).__new__(cls)
        return cls._instance

    def __init__(self, log_level='DEBUG',
                 name=os.path.split(os.path.splitext(sys.argv[0])[0])[-1],
                 log_file_name=datetime.datetime.now().strftime("%Y-%m-%d.log"),
                 log_path=os.path.join(R_ROOT_PATH, 'logs'),
                 log_file: bool = True,
                 ):
        if 'logger' not in self.__dict__:
            logger = logging.getLogger(name)
            logger.setLevel(log_level)

            if not os.path.exists(log_path):
                os.makedirs(log_path)

            if log_file:
                # 日志写入文件
                # 使用MidnightRotatingFileHandler
                file_handler = MidnightRotatingFileHandler(os.path.join(log_path, log_file_name))
                file_handler.setFormatter(logging.Formatter(file_app_format))
                logger.addHandler(file_handler)

            self.logger = logger
            # 禁止传递给父级logger
            self.logger.propagate = False

    def get_logger(self):
        return self.logger


class MidnightRotatingFileHandler(logging.FileHandler):
    """
    按时间-午夜-原子切割日志 handler
    """

    def __init__(self, filename):
        self._filename = filename
        self._rotate_at = self._next_rotate_datetime()
        super(MidnightRotatingFileHandler, self).__init__(filename, mode='a', encoding='utf-8')

    @staticmethod
    def _next_rotate_datetime():
        # rotate at midnight
        now = datetime.datetime.now()
        return now.replace(hour=0, minute=0, second=0) + datetime.timedelta(days=1)

    def _open(self):
        # now = datetime.datetime.now()
        # log_today = "%s.%s" % (now.strftime('%Y-%m-%d'), self._filename)
        log_today = "%s" % self._filename
        try:
            # create the log file atomically
            fd = os.open(log_today, os.O_CREAT | os.O_EXCL)
            # if coming here, the log file was created successfully
            os.close(fd)
        except OSError as e:
            if e.errno != errno.EEXIST:
                # should not happen
                raise e
        self.baseFilename = log_today
        return super(MidnightRotatingFileHandler, self)._open()

    def emit(self, record):
        now = datetime.datetime.now()
        if now > self._rotate_at:
            # time to rotate
            self._rotate_at = self._next_rotate_datetime()
            self.close()
        super(MidnightRotatingFileHandler, self).emit(record)


log = LoggingConfig(name='CA').get_logger()
log.propagate = True
